<?php

	define('FPDF_FONTPATH','../fpdf/font/');

require('../fpdf/mysql_table.php');
include_once('../conf/conf.mysql.php');




class PDF extends PDF_MySQL_Table
{
   

// Cargar los datos
function LoadData($file)
{
	// Leer las líneas del fichero
	$lines = file($file);
	$data = array();
	foreach($lines as $line)
		$data[] = explode(';',trim($line));
	return $data;
}


// Cabecera de pagina
function Header()
{
	// Logo
	$this->Image('../images/logo.gif',10,8,20,10);
	// Arial bold 15
	$this->SetFont('Arial','B',15);
	// Movernos a la derecha
	$this->Cell(60);
	// T�tulo
	$this->Cell(60,10,'ORDEN DE PEDIDO',1,0,'C');
	// Salto de l�nea
	$this->Ln(20);
        
}

// Pie de pagina
function Footer()
{
	// Posici�n: a 1,5 cm del final
	$this->SetY(-15);
	// Arial italic 8
	$this->SetFont('Arial','I',8);
	// Número de página
	$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
	$this->SetY(-15);
	$this->Cell(0,10,'REPORTE DE VENTAS',0,0,'L');
}

// Tabla coloreada
function FancyTable($header, $data)
{
	// Colores, ancho de l�nea y fuente en negrita
	$this->SetFillColor(255,0,0);
	$this->SetTextColor(255);
	$this->SetDrawColor(128,0,0);
	$this->SetLineWidth(.3);
	$this->SetFont('','B');
	// Cabecera
	$w = array(
        15,15,8,8,25,
        8,10,15,12,12,
        12,10,10,15,5
        );

    for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
	$this->Ln();
	// Restauracion de colores y fuentes
	$this->SetFillColor(224,235,255);
	$this->SetTextColor(0);
	$this->SetFont('');
	// Datos
	$fill = false;
    
    

    while($row = mysql_fetch_array($data))
    {
		$this->Cell($w[0],6,$row['marca'],'LR',0,'L',$fill);
		$this->Cell($w[1],6,$row['packing'],'LR',0,'L',$fill);
		$this->Cell($w[2],6,$row['tipo_caja'],'LR',0,'R',$fill);
		$this->Cell($w[3],6,$row['caja'],'LR',0,'R',$fill);
        $this->Cell($w[4],6,$row['variedad'],'LR',0,'L',$fill);
        
        $this->Cell($w[5],6,$row['longitud'],'LR',0,'R',$fill);
        $this->Cell($w[6],6,'','LR',0,'L',$fill);
        $this->Cell($w[7],6,'','LR',0,'L',$fill);
        $this->Cell($w[8],6,$row['tallos'],'LR',0,'L',$fill);
        $this->Cell($w[9],6,$row['cantidad'],'LR',0,'L',$fill);
        
        $this->Cell($w[10],6,'','LR',0,'L',$fill);
        $this->Cell($w[11],6,$row[11],'LR',0,'L',$fill);
        $this->Cell($w[12],6,$row[12],'LR',0,'L',$fill);
        $this->Cell($w[13],6,$row['finca'],'LR',0,'L',$fill);
        $this->Cell($w[14],6,$row[14],'LR',0,'R',$fill);
        
		$this->Ln();
		$fill = !$fill;
	}
	// Linea de cierre
	$this->Cell(array_sum($w),0,'','T');
}
}
//fin clase

//header('Content-Type: text/html; charset=UTF-8');

$packing = $_REQUEST['packing'];


// Creacion del objeto de la clase heredada
$pdf = new PDF();
//$pdf->DefOrientation = 'L';
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',8);

//---------------------------------------------

    $mysql  = new MySQL();;

    $sql = "SELECT pac.prep_id,
	pac.orden,
	pac.packing,
	pac.marca,
	pac.tipo_caja,
	pac.caja,
	pro.nombre AS producto,
	var.nombre AS variedad,
	pac.longitud,
	bod.nombre AS bodega,
	pac.tallos AS tallosBunche,
	0 AS bunchesCaja,
	(pac.bunches * pac.tallos) AS tallos,
	0 AS precioFob,
	0 AS precioCif,
	0 AS totalFob,
	0 AS totalCif
	FROM `prepacking` pac INNER JOIN `producto` pro ON pac.producto_id = pro.producto_id
	INNER JOIN variedad var ON var.variedad_id = pac.variedad_id 
	INNER JOIN bodega bod ON bod.bodega_id = pac.bodega_id 
    WHERE pac.packing= $packing ORDER BY pac.prep_id LIMIT 1 ";
    
    $result=$mysql->consulta($sql);
    $row = $mysql->fetch_array($result);
    
    $alto = 4;
    
    $pdf->Cell(20,$alto,"Pedido:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(20,$alto,$row['orden'],0,0,'L',false);
    $pdf->SetFont('Arial','',8);
    
    $pdf->Cell(15);
    $pdf->Cell(20,$alto,"Salida de Finca:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,"2012-06-01",0,0,'L',false);

    $pdf->Cell(15);
    $pdf->Cell(20,$alto,"Desde:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,"Quito",0,1,'L',false);
    
    
    
    $pdf->Cell(20,$alto,"Fecha Pedido:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,"2012-08-19",0,0,'L',false);
    
    $pdf->Cell(15);
    $pdf->Cell(20,$alto,"Salida de Vuelo:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,"2012-06-01",0,0,'L',false);

    $pdf->Cell(15);
    $pdf->Cell(20,$alto,utf8_decode("País:"),0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,utf8_decode("Rusia - Moscú"),0,1,'L',false);

    
    
    $pdf->Cell(20,$alto,"Cliente:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,$row['orden'],0,0,'L',false);

    $pdf->Cell(75);
    $pdf->Cell(20,$alto,utf8_decode("Con capuchón:"),0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,utf8_decode("NO"),0,1,'L',false);
    
   
    
    $pdf->Cell(20,$alto,"Notificar:",0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,$row['orden'],0,0,'L',false);
    
    $pdf->Cell(75);
    $pdf->Cell(20,$alto,utf8_decode("Pedido Parcial:"),0,0,'L',false);
    $pdf->Cell(5);
    $pdf->Cell(20,$alto,utf8_decode("NO"),0,1,'L',false);
    
    $pdf->Ln(10);

    
//---------------------------------------------

//for($i=1;$i<=10;$i++)
//	$pdf->Cell(0,6,utf8_decode('Imprimiendo línea número ').$i,0,1);
//$pdf->AddPage();
// Titulos de las columnas
$header = array(utf8_decode('Marca'),'Packing','Tipo','Caja',
    'Variedad','Lon','Recl','Envoltura','Tallos','Cantidad','Precio','Valor','Pen','Finca',utf8_decode('Día'));
// Carga de datos
//$data = $pdf->LoadData('paises.txt');


    $mysql  = new MySQL();;

    $sql = "SELECT pac.prep_id,
	pac.orden,
	pac.packing,
	pac.marca,
	pac.tipo_caja,
	pac.caja,
	pro.nombre AS producto,
	var.nombre AS variedad,
	pac.longitud,
	bod.nombre AS bodega,
	fin.nombre AS finca,
	pac.tallos AS tallosBunche,
	0 AS bunchesCaja,
	(pac.bunches * pac.tallos) AS tallos,
	pac.bunches AS cantidad,
	0 AS precioFob,
	0 AS precioCif,
	0 AS totalFob,
	0 AS totalCif
    FROM `prepacking` pac INNER JOIN `producto` pro ON pac.producto_id = pro.producto_id
	INNER JOIN variedad var ON var.variedad_id = pac.variedad_id 
	INNER JOIN bodega bod ON bod.bodega_id = pac.bodega_id 
	INNER JOIN finca fin ON fin.finca_id = pac.finca_id 
    WHERE pac.packing= $packing ORDER BY pac.prep_id  ";
    
    $result=$mysql->consulta($sql);
    


    //echo print_r($data);


$pdf->FancyTable($header,$result);
	
$pdf->Output();
?>
